package Demo0530;

import java.util.List;
import java.util.Scanner;

public class Test {
        private static Demo demo = new Demo();
        private static Scanner scanner = new Scanner(System.in);

        public static void main(String[] args) {
            // 初始化JDBC驱动
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                System.err.println("找不到JDBC驱动!");
                e.printStackTrace();
                return;
            }

            boolean running = true;
            while (running) {
                System.out.println("\n===== 员工管理系统 =====");
                System.out.println("1. 添加员工");
                System.out.println("2. 查看所有员工");
                System.out.println("3. 根据ID查询员工");
                System.out.println("4. 更新员工信息");
                System.out.println("5. 删除员工");
                System.out.println("0. 退出");
                System.out.print("请选择操作: ");

                int choice = scanner.nextInt();
                scanner.nextLine(); // 消耗换行符

                switch (choice) {
                    case 1:
                        addEmployee();
                        break;
                    case 2:
                        listAllEmployees();
                        break;
                    case 3:
                        getEmployeeById();
                        break;
                    case 4:
                        updateEmployee();
                        break;
                    case 5:
                        deleteEmployee();
                        break;
                    case 0:
                        running = false;
                        break;
                    default:
                        System.out.println("无效的选择，请重新输入!");
                }
            }

            scanner.close();
            System.out.println("系统已退出!");
        }

        private static void addEmployee() {
            System.out.println("\n--- 添加员工 ---");
            System.out.print("请输入员工编号: ");
            int id = scanner.nextInt();
            scanner.nextLine();

            System.out.print("请输入员工姓名: ");
            String name = scanner.nextLine();

            System.out.print("请输入员工密码: ");
            String password = scanner.nextLine();

            System.out.print("请输入员工年龄: ");
            int age = scanner.nextInt();
            scanner.nextLine();

            Emp emp = new Emp(id, name, password, age);
            if (demo.addEmp(emp)) {
                System.out.println("员工添加成功!");
            } else {
                System.out.println("员工添加失败!");
            }
        }

        private static void listAllEmployees() {
            System.out.println("\n--- 所有员工列表 ---");
            List<Emp> employees = demo.getAllEmployees();
            if (employees.isEmpty()) {
                System.out.println("没有员工记录!");
            } else {
                for (Emp emp : employees) {
                    System.out.println(emp);
                }
            }
        }

        private static void getEmployeeById() {
            System.out.println("\n--- 根据ID查询员工 ---");
            System.out.print("请输入员工ID: ");
            int id = scanner.nextInt();
            scanner.nextLine();

            Emp emp = demo.getEmployeeById(id);
            if (emp != null) {
                System.out.println("查询结果: " + emp);
            } else {
                System.out.println("没有找到ID为 " + id + " 的员工!");
            }
        }

        private static void updateEmployee() {
            System.out.println("\n--- 更新员工信息 ---");
            System.out.print("请输入要更新的员工ID: ");
            int id = scanner.nextInt();
            scanner.nextLine();

            Emp emp = demo.getEmployeeById(id);
            if (emp == null) {
                System.out.println("员工不存在!");
                return;
            }

            System.out.println("当前员工信息: " + emp);
            System.out.print("请输入新姓名(直接回车保持原值): ");
            String name = scanner.nextLine();
            if (!name.isEmpty()) {
                emp.setName(name);
            }

            System.out.print("请输入新密码(直接回车保持原值): ");
            String password = scanner.nextLine();
            if (!password.isEmpty()) {
                emp.setPassword(password);
            }

            System.out.print("请输入新年龄(输入0保持原值): ");
            int age = scanner.nextInt();
            scanner.nextLine();
            if (age != 0) {
                emp.setAge(age);
            }

            if (demo.updateEmployee(emp)) {
                System.out.println("员工信息更新成功!");
            } else {
                System.out.println("员工信息更新失败!");
            }
        }

        private static void deleteEmployee() {
            System.out.println("\n--- 删除员工 ---");
            System.out.print("请输入要删除的员工ID: ");
            int id = scanner.nextInt();
            scanner.nextLine();

            if (demo.deleteEmployee(id)) {
                System.out.println("员工删除成功!");
            } else {
                System.out.println("员工删除失败!");
            }
        }
    }

